<?php
//占位
trait trait_gdtext
{
//-------------------------------------------------------------------------------
	//位置
	//$b是离边框的距离
	protected function textxy($pos = 'rb', $w, $h, $b = 5)
	{
		if(is_array($pos))
		{
			list($x, $y) = $pos;
			$y += $h;
		}
		else
		{
	  	switch($pos)
	  	{
	  		//中间
	  		case 'm':
	  			$x = ($this->sw - $w)/2;
	  			$y = ($this->sh - $h)/2;
	  		break;
	  		//左上
	  		case 'lt':
	  			$x = $b;
	  			$y = $b;
	  		break;
	  		//右上
	  		case 'rt':
	  			$x = $this->sw - $w - $b;
	  			$y = $b;
	  		break;
	  		
	  		//左下
	  		case 'lb':
	  			$x = $b;
	  			$y = $this->sh - $h - $b;
	  		break;
	  		
	  		//右下
	  		case 'rb':
	  			$x = $this->sw - $w - $b;
	  			$y = $this->sh - $h - $b;
	  		break;
	  		
	  		//随机
	  		default:
	  			$x = mt_rand($b, $this->sw - $w - $b);
	  			$y = mt_rand($b, $this->sh - $h - $b);
	  		break;
	  	}
	  }
  	
  	return array($x, $y);
	}
	// --------------------------------------------------------------------
	//计算特定的字体大小
	protected function textSize($params)
	{
			list($size, $fontpath) = $params;
			
			if($size == '')
			{
				if($fontpath != '' && is_file($fontpath))
					$size = imageloadfont($fontpath);
				else
					$size = 5;
			}
			
			return $size;
	}
	// --------------------------------------------------------------------
	protected function _pt($arr)
	{
			list($x1, $y1, $x2, $y2) = $arr;
			$w = $x2 - $x1;
			$h = $y1 - $y2;
			return array($w, $h);
	}
	// --------------------------------------------------------------------
	protected function _ft($arr)
	{
		return array($arr[2] - $arr[0], $arr[1] - $arr[7]);
	}
	// --------------------------------------------------------------------
	//计算字体大小
	protected function textwh($text, $fonttype = '', $params = null)
	{
		if(!is_array($params)) $params = array();

		if($fonttype == 'pt')
		{
			list($fontpath, $size, $space, $tightness , $angle) = $params;
			
			if(!_fh($fontpath))
				_log1("{$fontpath} 不存在的路径");
			
			$arr = imagepsbbox($text, imagepsloadfont($fontpath), $size, $space, $tightness, $angle);
			return $this->_pt($arr);
		}
		else if($fonttype == 'ft')
		{
			list($fontpath, $size, $angle) = $params;
			
			if(!_fh($fontpath))
				_log1("{$fontpath} 不存在的路径");
			
			$arr = imageftbbox($size, $angle, $fontpath, $text);
			return $this->_ft($arr);
		}
		else if($fonttype == 'tt')
		{
			list($fontpath, $size, $angle) = $params;
			
			if(!_fh($fontpath))
				_log1("{$fontpath} 不存在的路径");
			
			$arr = imagettfbbox($size, $angle, $fontpath, $text);
			return $this->_ft($arr);
		}
		else
		{
			list($size, $fontpath, $space) = $params;
			$size = $this->textSize($params);
			$l = strlen($text);
			$w = imagefontwidth($size) * $l + $space * ($l - 1);
			$h = imagefontheight($size);
			return array($w, $h);
		}
	}
	// --------------------------------------------------------------------
	//TrueType,PostScript Type,或者是内置
	//pos是指位置array(x, y)
	//params 是 $fontpath, 字体大小, 字间隔, 加在字左右的空白宽 , 旋转角度
	function text($text, $pos = null, $color = '', $fonttype = '', $params = null)
	{		
		$color = $this->color2index($color);
		
		list($w, $h) = $this->textwh($text, $fonttype, $params);
		list($x, $y) = $this->textxy($pos, $w, $h);
		
		//.pfb
		if($fonttype == 'pt')
		{
			list($fontpath, $size, $space, $tightness , $angle, $backcolor, $antialias_steps) = $params;
			$font = imagepsloadfont($fontpath);
			$arr = imagepstext($this->handle, $text, $font, $size, $color, $backcolor, $x , $y ,$space, $tightness, $angle, $antialias_steps);
		}
		else if($fonttype == 'ft')	//FreeType 2
		{
			list($fontpath, $size, $angle) = $params;
			$arr = imagefttext($this->handle, $size, $angle, $x, $y, $color, $fontpath, $text);
		}
		else if($fonttype == 'tt')	//.ttf
		{
			list($fontpath, $size, $angle) = $params;
			$arr = imagettftext($this->handle, $size, $angle, $x, $y, $color, $fontpath, $text);
		}
		else
		{
			//list($size, $fontpath, $space) = $params;
			$size = $this->textSize($params);
			imagestring($this->handle, $size, $x, $y - $h, $text, $color);
		}
		
		return array($w, $h);
	}
}